package com.punch.order.design.strategy;


import com.alibaba.fastjson.JSONObject;
import com.punch.common.dto.order.OrderDTO;
import com.punch.common.enums.order.OrderStatusEnum;
import com.punch.common.exception.BusinessException;
import com.punch.order.design.PayStrategy;
import com.punch.order.exception.OrderExceptionEnum;
import com.punch.order.service.CommonOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 0元支付订单
 */
@Component
@Slf4j
public class ZeroStrategy implements PayStrategy {

    @Resource
    @Lazy
    private CommonOrderService commonOrderService;

    /**
     * 生成统一签名订单信息
     *
     * @param orderDTO 订单信息
     * @return 订单信息
     */
    @Override
    public JSONObject payOrder(OrderDTO orderDTO) throws BusinessException {
        log.info("处理0元订单");
        String orderSN = orderDTO.getOrderSN();
        if (orderDTO.getTotalAmount() != 0) {
            throw new BusinessException(OrderExceptionEnum.ZERO_ORDER_AMOUNT);
        }
        //修改订单状态
        commonOrderService.updateOrderStatus(orderSN, OrderStatusEnum.HAVE_PAY.getCode());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderSN", orderSN);
        return jsonObject;
    }

    /**
     * 支付回调
     *
     * @param request  请求
     * @param response 返回
     * @return string
     */
    @Override
    public String payNotify(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        throw new BusinessException(OrderExceptionEnum.PAY_CHANNEL_NOT_SUPPORT);
    }
}
